package com.fenghongzhang.day012;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Timer;
import java.util.TimerTask;

public class Main4Activity extends AppCompatActivity {

    private TextView text;
    private Button update;

    private static final String TAG = "Main4Activity";
    //在那个线程里面new对象,就运行在那个线程中.
    private Handler handler  = new Handler(){
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
                if(msg.what == 100){
                    String s = (String) msg.obj;
                    text.setText(s);
                    //取bundle
                    Bundle  b = msg.getData();
                    String s1 = b.getString("name");
                    Log.i(TAG, "handleMessage: "+s1);
                }else if(msg.what == 120){
                    Timer timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            Log.i(TAG, "run: ");
                        }
                    },0,1000);
                }

//            text.setText(?);
        }
    };
    private Button send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);


        text = (TextView) findViewById(R.id.text);
        update = (Button) findViewById(R.id.update);

        send = (Button) findViewById(R.id.send);

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Message message = Message.obtain();//get
//                message.what = 120;
//                handler.sendMessage(message);

                //发送一个空消息
                handler.sendEmptyMessage(120);
            }
        });


        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //从网下一个字符串
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //
                        final String s = "哈哈";
                        //发送到主线程中.
//                        handler.post(new Runnable() {
////                            @Override
////                            public void run() {
////                                text.setText(s);
////                            }
////                        });
                        //传递数据,用message 来封装
                        Message message = Message.obtain();//get
                        message.what = 100;
                        message.obj = s;
                        message.arg1 = 1;

                        Bundle bundle = new Bundle();
                        bundle.putString("name","小明");
                        message.setData(bundle);

                        handler.sendMessage(message);

                    }
                }).start();
            }
        });

    }
}
